<?php
namespace app\adminxcx\controller;
use \think\Controller;
use app\admin\model\Card as CardModel;
use app\admin\model\User as UserModel;

class Login extends Controller
{

    protected $CardModel;
    protected $UserModel;

    public function __construct()
    {
        $this->CardModel = new CardModel();
        $this->UserModel = new UserModel();
    }

    public function index()
    {
        if(request()->isPost()){ 
            $data = [];
            $uid = session('uid');
            $userRes =  $this->UserModel->get($uid);
            if($userRes->cid){
                $data['state'] = -1;
                return $data;
            }
            $cndata = [
                'name' => input('name'),
                'phone' => input('tel')
            ];
            $endata = [
                'name_en' => input('name'),
                'phone' => input('tel')
            ];
            $cardRes = $this->CardModel->where($cndata)->find();
            if($cardRes){
                $userRes->cid = $cardRes->id;
                $res = $userRes->save();
                $token = settoken();
                cache('token_admin'.$uid,$token,1800);
                cache('token'.$uid,null);
                $data = [
                    'state' => 1,
                    'uid' => $userRes->id,
                    'token' => $token
                ];
                return $data;
            }else{
                $cardRes = $this->CardModel->where($endata)->find();
                if($cardRes){
                    $userRes->cid = $cardRes->id;
                    $userRes->save();
                    $token = settoken();
                    cache('token_admin'.$uid,$token,1800);
                    cache('token'.$uid,null);
                    $data = [
                        'state' => 1,
                        'uid' => $userRes->id,
                        'token' => $token
                    ];
                    return $data;
                }else{
                    $data['state'] = 0;
                    return $data;
                }
            }
        }
        return view();
    }

 
}
